package lixiang.neurons;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class DataView {

	private Canvas canvas;
	private GC gc;
	private Display display;
	private Shell shell;
	private Image image;

	private int[] data;
	private int max, min, count;

	private int margin, height, width, x = 0, y = 0;

	public DataView(int[] data) {
		this.display = new Display();
		this.data = data;
	}

	public void updateView() {
		if (display == null || display.isDisposed()) {
			return;
		}
		display.asyncExec(new Runnable() {
			public void run() {
				if (canvas.isDisposed())
					return;
				canvas.redraw();
			}
		});
	}

	public void show() {
		init();
		shell = new Shell(display);
		shell.setText("Data View");
		shell.setSize(width + margin * 3, height + margin * 4);
		createContents();
		shell.open();
		draw();
		canvas.redraw();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

	private void init() {
		width = 800;
		height = 600;
		margin = 20;
		count = data.length;
		for (int i = 0; i < count; i++) {
			if (data[i] > max)
				max = data[i];
			if (data[i] < min)
				min = data[i];
		}
		System.out.println("count: " + count + ", max: " + max + ", min: "
				+ min);
	}

	private void draw() {
		x = margin;
		y = margin;
		gc.setBackground(display.getSystemColor(SWT.COLOR_GRAY));
		// gc.fillRectangle(x, y, width, height);
		gc.drawLine(x, y + height, x + width, y + height);
		gc.drawLine(x, y, x, y + height);
		for (int i = 0; i < count; i++) {
			int z = data[i];
			y = height - (z - min) * height / max + margin;
			x = i * width / count + margin;
			System.out.println("x: " + x + ", y: " + y);
			gc.drawPoint(x, y);
			gc.drawPoint(x + 1, y + 1);
			gc.drawPoint(x - 1, y - 1);
			gc.drawPoint(x + 1, y - 1);
			gc.drawPoint(x - 1, y + 1);
		}
	}

	private void createContents() {
		canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
		canvas.setSize(width + margin * 2, height + margin * 2);
		image = new Image(display, canvas.getBounds());
		gc = new GC(image);
		gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				draw();
				e.gc.drawImage(image, 0, 0);
			}
		});
		canvas.addMouseListener(new MouseListener() {
			public void mouseDown(MouseEvent e) {
			}

			public void mouseUp(MouseEvent e) {
			}

			public void mouseDoubleClick(MouseEvent e) {
			}
		});
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new DataView(new int[0]).show();
	}

}
